package controllers

import (
	"IoTHubAddressService/iotserver/models"
	"encoding/json"
	"github.com/astaxie/beego"
)

type LoginController struct {
	beego.Controller
}
// @Title ServerLogin
// @Description Login
// @Param	body		body 	models.User	true		"body for login"
// @Success 201 {object} models.ResponseType
// @Failure 204 error
// @router / [post]
func (l *LoginController)Login()  {
	var user models.User
	json.Unmarshal(l.Ctx.Input.RequestBody,&user)
	token,err:=models.Login(user)
	if err!=nil{
		ts:=models.ResponseType{
			SuccessCode:0,
			MessageCode:2002,
			Token:err.Error(),
		}
		l.Data["json"]=&ts
	}else {
		ts:=models.ResponseType{
			SuccessCode:1,
			MessageCode:2001,
			Token:token,
		}
		l.Ctx.ResponseWriter.WriteHeader(201)
		l.Data["json"]=&ts
	}
	l.ServeJSON()
}